package cn.ac.jaas.isc.vegetable.auth.config;

import cn.ac.jaas.isc.vegetable.common.security.component.VegetableCommenceAuthExceptionEntryPoint;
import cn.ac.jaas.isc.vegetable.common.security.component.VegetableWebResponseExceptionTranslator;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableCustomTokenServices;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;

import java.util.Collections;

/**
 * @author 张高昌
 * @date 2022/4/24 9:16
 * @description: 认证服务器配置
 */
@Configuration
@AllArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private final ClientDetailsService vegetableClientDetailsServiceImpl;

    private final AuthenticationManager authenticationManagerBean;

    private final UserDetailsService vegetableUserDetailsService;

    private final AuthorizationCodeServices authorizationCodeServices;

    private final TokenStore redisTokenStore;

    private final TokenEnhancer tokenEnhancer;

    private final ObjectMapper objectMapper;

    @Override
    @SneakyThrows
    public void configure(ClientDetailsServiceConfigurer clients) {
        clients.withClientDetails(vegetableClientDetailsServiceImpl);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
        oauthServer.allowFormAuthenticationForClients()
                .authenticationEntryPoint(new VegetableCommenceAuthExceptionEntryPoint(objectMapper))
                .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST).tokenServices(tokenServices())
                .tokenStore(redisTokenStore).tokenEnhancer(tokenEnhancer).userDetailsService(vegetableUserDetailsService)
                .authorizationCodeServices(authorizationCodeServices).authenticationManager(authenticationManagerBean)
                .reuseRefreshTokens(false).pathMapping("/oauth/confirm_access", "/token/confirm_access")
                .exceptionTranslator(new VegetableWebResponseExceptionTranslator());
    }

    @Bean
    public VegetableCustomTokenServices tokenServices() {
        VegetableCustomTokenServices tokenServices = new VegetableCustomTokenServices();
        tokenServices.setTokenStore(redisTokenStore);
        tokenServices.setSupportRefreshToken(true);
        tokenServices.setReuseRefreshToken(false);
        tokenServices.setClientDetailsService(vegetableClientDetailsServiceImpl);
        tokenServices.setTokenEnhancer(tokenEnhancer);
        addUserDetailsService(tokenServices, vegetableUserDetailsService);
        return tokenServices;
    }

    private void addUserDetailsService(VegetableCustomTokenServices tokenServices, UserDetailsService userDetailsService) {
        if (userDetailsService != null) {
            PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
            provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(userDetailsService));
            tokenServices.setAuthenticationManager(new ProviderManager(Collections.singletonList(provider)));
        }
    }
}
